package net.apps123.soundmeter;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;

import android.widget.ListView;
import net.apps123.view.SplAdapter;
import net.apps123.view.SplDataBean;
import net.apps123.view.SplView;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Set;

public class RemoteActivity extends Activity {
    private ReceiveBroadCast receiveBroadCast;

    private Context mContext;
    private SplView splView;
    private ListView remoteSplList;
    private List<SplDataBean> splList = new ArrayList<>();
    private SplAdapter splAdapter ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_remote);

        splView = findViewById(R.id.splView);
        remoteSplList = findViewById(R.id.remoteSplList);

        registerBroadCast();

        splList = new ArrayList<>();
        splAdapter = new SplAdapter(this, splList);
        remoteSplList.setAdapter(splAdapter);

    }


    private void registerBroadCast(){
        /** 注册广播 */
        receiveBroadCast = new ReceiveBroadCast();
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.gasFragment");    //只有持有相同的action的接受者才能接收此广播
        registerReceiver(receiveBroadCast, filter);

    }

    class ReceiveBroadCast extends BroadcastReceiver {

        HashMap<String, short[]> deviceList = new HashMap<>();

        @Override
        public void onReceive(Context context, Intent intent) {
            //得到广播中得到的数据，并显示出来
//            String gasname = intent.getExtras().getString("data");
            byte[] dataArr = intent.getExtras().getByteArray("data");
            Log.d("=======", "ReceiveBroadCast==" + dataArr.length);
            String ipStr = intent.getStringExtra("ipStr");
            if (dataArr == null || dataArr.length < 1278){
                return;
            }
            short [] buffer = new short[dataArr.length / 2];
            ByteBuffer.wrap(dataArr).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(buffer);

            deviceList.put(ipStr, buffer);


            if (splView != null){
                splView.updateView(buffer, "");
            }

//            splList = new ArrayList<>();
            splList.clear();

            Set<String> keySet = deviceList.keySet();
            for (String key : keySet) {
                System.out.println("key:" + key + " , value:" );
//                splList.add(deviceList.get(key));
                SplDataBean splDataBean = new SplDataBean(key, deviceList.get(key));
                splList.add(splDataBean);
            }

//            splList.add(buffer);
//            splList.add(buffer);
//            splList.add(buffer);
//            splList.add(buffer);
            splAdapter.notifyDataSetChanged();
//            splAdapter = new SplAdapter(RemoteActivity.this, splList);
//            remoteSplList.setAdapter(splAdapter);
        }
    }


    @Override
    protected void onDestroy() {
        unregisterReceiver(receiveBroadCast);
        super.onDestroy();
    }
}
